package cn.lingyangwl.agile.infra.gen.controller;

import cn.lingyangwl.agile.infra.gen.model.entity.*;
import cn.lingyangwl.agile.infra.gen.model.rqrs.*;
import cn.lingyangwl.agile.infra.gen.service.*;
import cn.lingyangwl.agile.ram.*;
import cn.lingyangwl.framework.core.response.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author shenguangyang
 */
@Api(tags = "代码模板组")
@RestController
@RequestMapping("/codeTemplateGroup")
@RequiredArgsConstructor
public class TemplateGroupController {
    private final TemplateGroupService templateGroupService;

    @ApiOperation(value = "查询代码模板组列表")
    @RamResource(action = "lcp:templateGroup:query", name = "低代码-查询代码模板组", type = ActionType.READ)
    @GetMapping("listGroup")
    public Resp<List<TemplateGroupResp>> listTemplateGroup(TemplateGroupListReq rq) {
        return Resp.ok(templateGroupService.listTemplateGroup(rq));
    }

    @ApiOperation(value = "查询代码模板组详情")
    @RamResource(action = "lcp:templateGroup:query", name = "低代码-查询代码模板组", type = ActionType.READ)
    @GetMapping("getGroup")
    public Resp<TemplateGroup> get(@RequestParam("id") Long id) {
        TemplateGroup group = templateGroupService.getById(id);
        return Resp.ok(group);
    }

    @ApiOperation(value = "保存代码模板组")
    @RamResource(action = "lcp:templateGroup:save", name = "低代码-保存代码模板组", type = ActionType.WRITE)
    @PostMapping("saveGroup")
    public Resp<TemplateGroup> save(@RequestBody TemplateGroup templateGroupPO) {
        templateGroupService.save(templateGroupPO);
        return Resp.ok(templateGroupPO);
    }

    @ApiOperation(value = "更新代码模板组")
    @RamResource(action = "lcp:templateGroup:update", name = "低代码-更新代码模板组", type = ActionType.WRITE)
    @PostMapping("updateGroup")
    public Resp<Object> update(@RequestBody TemplateGroup templateGroupPO) {
        TemplateGroup group = templateGroupService.getByName(templateGroupPO.getGroupName());
        if (group != null && !Objects.equals(group.getId(), templateGroupPO.getId())) {
            throw new RuntimeException(templateGroupPO.getGroupName() + " 已存在");
        }
        templateGroupService.updateById(templateGroupPO);
        return Resp.ok();
    }

    @ApiOperation(value = "删除代码模板组")
    @RamResource(action = "lcp:templateGroup:delete", name = "低代码-删除代码模板组", type = ActionType.WRITE)
    @DeleteMapping("deleteGroup")
    public Resp<Object> delete(@RequestBody TemplateGroup templateGroupPO) {
        templateGroupService.deleteGroup(templateGroupPO);
        return Resp.ok();
    }

    @ApiOperation(value = "批量删除代码模板组")
    @RamResource(action = "lcp:templateGroup:delete", name = "低代码-删除代码模板组", type = ActionType.WRITE)
    @DeleteMapping("batchDeleteGroup")
    public Resp<Object> deleteBatch(@RequestParam("ids") Long[] ids) {
        templateGroupService.deleteGroupBatch(Arrays.asList(ids));
        return Resp.ok();
    }
}